package cn.sumpu.app.guoguojie.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import cn.sumpu.app.guoguojie.biz.DoingBiz;
import cn.sumpu.app.guoguojie.biz.FeedBiz;
import cn.sumpu.app.guoguojie.constant.DoingType;
import cn.sumpu.app.guoguojie.constant.FeedIdType;
import cn.sumpu.app.guoguojie.constant.FeedType;
import cn.sumpu.app.guoguojie.dao.FeedDao;
import cn.sumpu.app.guoguojie.dao.FriendDao;
import cn.sumpu.app.guoguojie.domain.Doing;
import cn.sumpu.app.guoguojie.domain.Feed;
import cn.sumpu.app.guoguojie.domain.Ext.FeedExt;
import cn.sumpu.app.guoguojie.domain.Ext.FriendFamily;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.IdGenerator;

public class FeedBizImpl implements FeedBiz {

	private FeedDao feedDao;
	private FriendDao friendDao;
	@Autowired
	private DoingBiz doingBiz;

	public void setFriendDao(FriendDao friendDao) {
		this.friendDao = friendDao;
	}

	public void setFeedDao(FeedDao feedDao) {
		this.feedDao = feedDao;
	}

	public void insert(Feed feed) throws AppBusinessException {
		feed.setIdentifier(IdGenerator.createIdentifier());
		feedDao.insert(feed);
	}

	public List<FeedExt> getFeedByGroup(Map<String, Object> map)
			throws AppBusinessException {
		return feedDao.getFeedByGroup(map);
	}

	public void updateFeed(Feed feed) throws AppBusinessException {
		feedDao.updateFeed(feed);

	}

	public void deleteFeedById(String id) throws AppBusinessException {
		feedDao.deleteFeedById(id);

	}

	public Feed getFeedById(String id) throws AppBusinessException {

		return feedDao.getFeedById(id);
	}

	@Override
	public List<FriendFamily> getMyFriendsFamily(Map<String, Object> map)
			throws AppBusinessException {

		return friendDao.getMyFriendsFamily(map);
	}

	@Override
	public List<FeedExt> getMyFamilyFeedList(Map<String, Object> map)
			throws AppBusinessException {

		return feedDao.getMyFamilyFeedList(map);
	}

	@Override
	public void deleteBlogFeed(String blogId) throws AppBusinessException {
		feedDao.deleteBlogFeed(blogId);

	}

	@Override
	public int getFriendFamilyAllFeedListCount(Map<String, Object> map)
			throws AppBusinessException {

		return feedDao.getFriendFamilyAllFeedListCount(map);
	}

	@Override
	public int getFeedByGroupCount(Map<String, Object> map)
			throws AppBusinessException {

		return feedDao.getFeedByGroupCount(map);
	}

	@Override
	public void deleteAlbumFeed(String member, String albumId)
			throws AppBusinessException {
		if (Common.empty(member) || Common.empty(albumId))
			return;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("member", member);
		map.put("feedId", albumId);
		map.put("feedType", FeedType.ALBUM.getValue());
		map.put("feedIdType", FeedIdType.ALBUMID.getValue());

		feedDao.deleteFeed(map);
	}

	@Override
	public List<FeedExt> getDoingFeed(Map<String, Object> map)
			throws AppBusinessException {
		List<FeedExt> doingList = feedDao.getDoingFeed(map);
		if(doingList!=null && doingList.size()>0){
			for (FeedExt feedExt : doingList) {
				String type = feedExt.getType();
				if(DoingType.FORWARD.getValue().equals(type)){
					Doing rootDoing = doingBiz.getDoingById(feedExt.getRootId());
					feedExt.setRootDoing(rootDoing);
				}
			}
		} 
		return doingList;
	}

	@Override
	public List<FeedExt> getFriendFamilyAllFeedList(Map<String, Object> map)
			throws AppBusinessException {
		
		List<FeedExt> doingList = feedDao.getFriendFamilyAllFeedList(map);
		if(doingList!=null && doingList.size()>0){
			for (FeedExt feedExt : doingList) {
				String feedType = feedExt.getFeedType(); 
				if(FeedType.DOING.getValue().equals(feedType)){ 
					Doing rootDoing = doingBiz.getRootDoingById(feedExt.getFeedId());
					feedExt.setRootDoing(rootDoing);
				}
			}
		}
		return doingList;
	}

	@Override
	public int getFeedCount(Map<String, Object> map)
			throws AppBusinessException {

		return feedDao.getFeedCount(map);
	}

	public List<FeedExt> getDoingByClassify(Map<String, Object> map)
			throws AppBusinessException {
		List<FeedExt> doingList = feedDao.getDoingByClassify(map); 
		if(doingList!=null && doingList.size()>0){
			for (FeedExt feedExt : doingList) {
				String type = feedExt.getType();
				if(DoingType.FORWARD.getValue().equals(type)){
					Doing rootDoing = doingBiz.getDoingById(feedExt.getRootId());
					feedExt.setRootDoing(rootDoing);
				}
			}
		} 
		return doingList;
	}

	public List<FeedExt> getAllFeedByClassify(Map<String, Object> map)
			throws AppBusinessException {
		List<FeedExt> doingList = feedDao.getAllFeedByClassify(map);
		if(doingList!=null && doingList.size()>0){
			for (FeedExt feedExt : doingList) {
				String feedType = feedExt.getFeedType();
				if(FeedType.DOING.getValue().equals(feedType)){ 
					Doing rootDoing = doingBiz.getRootDoingById(feedExt.getFeedId());
					feedExt.setRootDoing(rootDoing);
				}
			}
		} 
		return doingList;
	}

	public List<FeedExt> getFeedByGroupClassify(Map<String, Object> map)
			throws AppBusinessException {
		return feedDao.getFeedByGroupClassify(map);
	}

	@Override
	public int getDoingFeedCount(Map<String, Object> map)
			throws AppBusinessException {
		return feedDao.getDoingFeedCount(map);
	}

	@Override
	public int getDoingByClassifyCount(Map<String, Object> map)
			throws AppBusinessException {
		return feedDao.getDoingByClassifyCount(map);
	}

	@Override
	public int getFeedByGroupClassifyCount(Map<String, Object> map)
			throws AppBusinessException {
		return feedDao.getFeedByGroupClassifyCount(map);
	}

	@Override
	public int getFriendFamilyAllFeedCount(Map<String, Object> map)
			throws AppBusinessException {
		return feedDao.getFriendFamilyAllFeedCount(map);
	}

	@Override
	public int getAllFeedByClassifyCount(Map<String, Object> map)
			throws AppBusinessException {
		return feedDao.getAllFeedByClassifyCount(map);
	}

	/**
	 * author Bruce Zhang
	 * @param map	门牌号和动态类型
	 * @return 他家的动态
	 * @throws AppBusinessException
	 */
	@Override
	public List<FeedExt> getFeedByDoorplate(Map<String, Object> map)
			throws AppBusinessException {
		List<FeedExt> doingList = feedDao.getFeedByDoorplate(map);
		if(doingList!=null && doingList.size()>0){
			for (FeedExt feedExt : doingList) {
				String feedType = feedExt.getFeedType(); 
				if(FeedType.DOING.getValue().equals(feedType)){ 
					Doing rootDoing = doingBiz.getRootDoingById(feedExt.getFeedId());
					feedExt.setRootDoing(rootDoing);
				}
			}
		}
		return doingList;
	}

	/**
	 * author Bruce 	Zhang
	 * @param String	门牌号
	 * @return 他家的动态的总记录数
	 * @throws AppBusinessException
	 */
	@Override
	public Integer getFeedByDoorplateCount(Map<String, Object> map)
			throws AppBusinessException {
		return feedDao.getFeedByDoorplateCount(map);
	}

}
